
#ifndef _XRJ_PROTOCOL_H
#define _XRJ_PROTOCOL_H

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "includes.h"
#include "xrj_mutex_lock.h"

#define TX_Num				5

#define		SOFTWARE_VESION		0x0e01	//修改超声波检测为数字量 2000 为空桶， 0为满桶
#define		HAREWARE_VESION		0x0007	
#define YEAR ((((__DATE__ [7] - '0') * 10 + (__DATE__ [8] - '0')) * 10 + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

#define MONTH (	  __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 1 : 6)\
				: __DATE__ [2] == 'b' ? 2 \
				: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
				: __DATE__ [2] == 'y' ? 5 \
				: __DATE__ [2] == 'l' ? 7 \
				: __DATE__ [2] == 'g' ? 8 \
				: __DATE__ [2] == 'p' ? 9 \
				: __DATE__ [2] == 't' ? 10 \
				: __DATE__ [2] == 'v' ? 11 : 12)

#define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 + (__DATE__ [5] - '0'))

#define TIME (__TIME__[0]- '0')*1000+(__TIME__[1]- '0')*100+(__TIME__[3]- '0')*10+(__TIME__[4]- '0')


#define YEAR_0 __DATE__ [7]
#define YEAR_1 __DATE__ [8]
#define YEAR_2 __DATE__ [9]
#define YEAR_3 __DATE__ [10]

#define MONTH_0  (__DATE__ [0] == 'D' ? '1'\
				: __DATE__ [2] == 't' ? '1' \
				: __DATE__ [2] == 'v' ? '1' \
				:'0')
#define MONTH_1  (	  __DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? '1' : '6')\
				: __DATE__ [2] == 'b' ? '2' \
				: __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? '3' : '4') \
				: __DATE__ [2] == 'y' ? '5' \
				: __DATE__ [2] == 'l' ? '7' \
				: __DATE__ [2] == 'g' ? '8' \
				: __DATE__ [2] == 'p' ? '9' \
				: __DATE__ [2] == 't' ? '0' \
				: __DATE__ [2] == 'v' ? '1' : '2')
				

#define DAY_0 (__DATE__ [4] == ' ' ? '0' : __DATE__ [4])
#define DAY_1 __DATE__ [5]

#define TIME_0 __TIME__[0]
#define TIME_1 __TIME__[1]
#define TIME_2 __TIME__[3]
#define TIME_3 __TIME__[4]

extern const char Version2Flash[256];







//协议部分=======================================

/* --- enums --- */

/*
 * 指令编码
 */
typedef enum _Iot__ActionCodes {
  /*
   * do nothing
   */
  IOT__ACTION_CODES__DO_NONE = 0,
  /*
   * 上报数据
   */
  IOT__ACTION_CODES__REPORT = 32,
  /*
   * 查询数据
   */
  IOT__ACTION_CODES__QUERY = 64,
  /*
   * 设置/控制
   */
  IOT__ACTION_CODES__SETTING = 96,
	
	/*
   * OTA
   */
  IOT__ACTION_CODES__OTA = 192,
  /*
   * 应答
   */
  IOT__ACTION_CODES__RESPONSE = 224,	
} Iot__ActionCodes;



/*
 * 外设索引
 */
typedef enum _Iot__UnitIndex {
  /*
   * 未知外设
   */
  IOT__UNIT_INDEX__UNKNOWN = 0,
  /*
   * 固件和硬件版本
   */
  IOT__UNIT_INDEX__ROM_VERSION = 1,
	
  IOT__UNIT_INDEX__OTA_START = 2,
  IOT__UNIT_INDEX__OTA_BYTES = 3,
	
  /*
   * 设备运行状态
   */
  IOT__UNIT_INDEX__RUNNING_STATUS = 6,
  /*
   * 异常上报
   */
  IOT__UNIT_INDEX__RUNNING_ERROR = 7,
	
  IOT__UNIT_INDEX__ALIVE = 9,	

  /*
   * 按键
   */
  IOT__UNIT_INDEX__KEYBOARD = 11,
  /*
   * IC卡
   */
  IOT__UNIT_INDEX__IC_CARD = 12,
  /*
   * 二维码
   */
  IOT__UNIT_INDEX__QR = 13,
  /*
   * 垃圾袋二维码
   */
  IOT__UNIT_INDEX__QR_TRASH = 14,
  /*
   * LED
   */
  IOT__UNIT_INDEX__LED_1 = 31,
  IOT__UNIT_INDEX__LED_2 = 32,
  /*
   * 按键
   */
  IOT__UNIT_INDEX__KEY_1 = 41,
  IOT__UNIT_INDEX__KEY_2 = 42,
  /*
   * 门
   */
  IOT__UNIT_INDEX__PLATE_1 = 51,
  IOT__UNIT_INDEX__PLATE_2 = 52,
  /*
   * 锁
   */
  IOT__UNIT_INDEX__LOCK_1 = 53,
  IOT__UNIT_INDEX__LOCK_2 = 54,
  /*
   * 货物通道
   */
  IOT__UNIT_INDEX__MULTI_AISLE = 55,
  /*
   * 风扇
   */
  IOT__UNIT_INDEX__FAN = 56,
  /*
   * 灯
   */
  IOT__UNIT_INDEX__LIGHT = 61,
  IOT__UNIT_INDEX__LIGHT_INSIDE_1 = 62,
  IOT__UNIT_INDEX__LIGHT_INSIDE_2 = 63,
  IOT__UNIT_INDEX__LIGHT_OUTSIDE_1 = 64,
  IOT__UNIT_INDEX__LIGHT_OUTSIDE_2 = 65,
  
   /*
   * 感应开关
   */
  IOT__UNIT_INDEX__SENSOR_1 = 66,
  IOT__UNIT_INDEX__SENSOR_2 = 67,
  /*
   * 称重传感器
   */
  IOT__UNIT_INDEX__WEIGHING_1 = 101,
  IOT__UNIT_INDEX__WEIGHING_2 = 102,
  /*
   * 温度
   */
  IOT__UNIT_INDEX__TEMPERATURE = 103,
  /*
   * 垃圾容量传感器
   */
  IOT__UNIT_INDEX__TRASH_CAPACITY_1 = 105,
  IOT__UNIT_INDEX__TRASH_CAPACITY_2 = 106,
  /*
   * 落料传感器
   */
  IOT__UNIT_INDEX__FALL = 110,
  /*
   * GPS控制板联合SN
   */
  IOT__UNIT_INDEX__GPS_UNIT_SN = 200,
  /*
   * GPS数据上报URL
   */
  IOT__UNIT_INDEX__GPS_PUSH_URL = 201,
  /*
   * IC卡扇区1
   */
  IOT__UNIT_INDEX__IC_BLOCK_2 = 222
} Iot__UnitIndex;


/*
 * 状态码
 */
typedef enum _Iot__StatusCodes {
  /*
   * 无效状态
   */
  IOT__STATUS_CODES__NONE = 0,
  /*
   * 状态反常
   */
  IOT__STATUS_CODES__ANOMALY = 1,
  /*
   * 开
   */
  IOT__STATUS_CODES__ON = 3,
  /*
   * 关
   */
  IOT__STATUS_CODES__OFF = 4,
  /*
   * 按下
   */
  IOT__STATUS_CODES__DOWN = 5,
  /*
   * 弹起
   */
  IOT__STATUS_CODES__UP = 6,
  /*
   * 上升沿
   */
  IOT__STATUS_CODES__RISING = 7,
  /*
   * 下降沿
   */
  IOT__STATUS_CODES__TRAILING = 8,
  /*
   * 空
   */
  IOT__STATUS_CODES__EMPTY = 10,
  /*
   * 满
   */
  IOT__STATUS_CODES__FULL = 11,
  /*
   * 执行中
   */
  IOT__STATUS_CODES__DOING = 12,
  /*
   * 开始
   */
  IOT__STATUS_CODES__START = 13,
  /*
   * 结束
   */
  IOT__STATUS_CODES__TERMINATE = 14,
  /*
   * 某种状态/动作已准备好
   */
  IOT__STATUS_CODES__READY = 15,
  /*
   * 设备忙碌
   */
  IOT__STATUS_CODES__BUSY = 16,
  /*
   * 激活
   */
  IOT__STATUS_CODES__ACTIVATE = 50,
  /*
   * 运行中
   */
  IOT__STATUS_CODES__IN_SERVICE = 51,
  /*
   * 休眠
   */
  IOT__STATUS_CODES__DORMANCY = 52
  
} Iot__StatusCodes;




typedef enum _Iot__ErrorCodes {
  /*
   * 通用异常
   */
  IOT__ERROR_CODES__ERROR = 0,
  /*
   * 不支持此操作
   */
  IOT__ERROR_CODES__NOT_SUPPORTED = 1,
  /*
   * 外设未安装/错误
   */
  IOT__ERROR_CODES__NOT_INSTALLED = 2,
  /*
   * 硬件不匹配
   */
  IOT__ERROR_CODES__HARDWARE_MISMATCH = 3,
  /*
   * ROM不匹配
   */
  IOT__ERROR_CODES__ROM_MISMATCH = 4,
  /*
   * 楼层异常
   */
  IOT__ERROR_CODES__ERR_FLOOR = 20,
  /*
   * 货道异常
   */
  IOT__ERROR_CODES__ERR_CHANNEL = 21,
  /*
   * 出货异常
   */
  IOT__ERROR_CODES__ERR_DELIVERY = 22,
  /*
   * 二维码异常
   */
  IOT__ERROR_CODES__ERR_QR = 23,
  /*
   * 落料异常
   */
  IOT__ERROR_CODES__ERR_FALL = 24,
  /*
   * 取料异常
   */
  IOT__ERROR_CODES__ERR_TAKE = 25
} Iot__ErrorCodes;


/*
 * 执行结果，用于指令反馈
 */
typedef enum _Iot__ResultCodes {
  /*
   * 无效结果
   */
  IOT__RESULT_CODES__RST_NONE = 0,
  /*
   * 错误
   */
  IOT__RESULT_CODES__RST_ERROR = 1,
  /*
   * 成功
   */
  IOT__RESULT_CODES__RST_SUCCESS = 2

} Iot__ResultCodes;

/*
 * 数据类型
 */
typedef enum _Iot__ValueTypes {
  IOT__VALUE_TYPES__INT32 = 0,
  IOT__VALUE_TYPES__INT64 = 1,
  IOT__VALUE_TYPES__UINT32 = 2,
  IOT__VALUE_TYPES__UINT64 = 3,
  IOT__VALUE_TYPES__SINT32 = 4,
  IOT__VALUE_TYPES__SINT64 = 5,
  IOT__VALUE_TYPES__FIXED32 = 6,
  IOT__VALUE_TYPES__FIXED64 = 7,
  IOT__VALUE_TYPES__SFIXED32 = 8,
  IOT__VALUE_TYPES__SFIXED64 = 9,
  IOT__VALUE_TYPES__BOOL = 10,
  IOT__VALUE_TYPES__FLOAT = 22,
  IOT__VALUE_TYPES__DOUBLE = 12,
  IOT__VALUE_TYPES__STRING = 13,
  IOT__VALUE_TYPES__BYTES = 14,
  IOT__VALUE_TYPES__STATUS = 15,
  IOT__VALUE_TYPES__SPECIAL = 16,
  IOT__VALUE_TYPES__VT_ERROR = 17
} Iot__ValueTypes;


//协议部分=======================================













enum {
	PROTO_SUCCESS = 0,	
	PROTO_ERROR = 1,
	NOT_SUPPORTED = 2,
	NOT_INSTALLED = 3,
	HARDWARE_MISMATCH = 4,
	ROM_MISMATCH = 5,
	PROTO_NONE = 6,
};

typedef enum _ProtoStatus{
	Proto_Tx_Free = 0,
	Proto_Tx_Busy = 1,
	Proto_Tx_ToFree = 2,
	Proto_Tx_FALSE = 3,
} ProtoStatus;

typedef enum _ProtoFlag{
	FeedBack_Enable  = 0,
	FeedBack_Disable = 1,
	Flag_Response = 2,
}ProtoFlag;

typedef struct _St_Proto_TxRx{
	XRJ_IOT_MUTEX_S Lock;
	UINT16 session;
	UINT8 BUF[1024];
	UINT32 len;
	struct {
		UINT32 session;
		void *TXbuf;
		UINT32 len;
		UINT32 Timeout;
		UINT32 TimeoutCnt;
		UINT32 TimeoutNum;
		ProtoStatus Status;
		ProtoFlag Flag;
	}Packet[TX_Num];
}St_Proto_TxRx;



typedef struct _St_Session_Flag{
	UINT16 session;
	UINT32 enable;
} St_Session_Flag;




typedef struct _Device_Descriptor
{
	/** 编号*/
	Iot__UnitIndex index;
	/** 数据类型*/ 
	Iot__ValueTypes valuetype;

	UINT32    actionarray;	
}Device_Descriptor; 

void ProtoTxRx_Create_Sem(void);
INT32 TxPacket_Init(UINT8 *buf, UINT32 len, UINT32 session, UINT32 TimeoutNum, ProtoFlag FalseFeedback);
void Protobuf_task(void *pdata);
INT32 Send_Report_Message(INT32 index, St_Session_Flag *Session_Flag);



enum{
	FLAG_OTA_IDLE = 0,
	FLAG_OTA_DOING = 1,
	FLAG_OTA_RESET = 2,
};

typedef struct __st_OTA{
	UINT8	WriteFlag[128];
	UINT8   Start;
	UINT32  vesion;
	UINT32	hashcode;
	UINT32  packettotal;
	UINT32	OnePacketsize;
	UINT32	Cnt_Reset;
}st_OTA;





/********************需修改部分*******sssss****************************************************/
typedef struct _st_Device_Value {
	XRJ_IOT_MUTEX_S 	Lock;
	
	INT8				VERSION[64];
	
	Iot__StatusCodes	KEY_1;
	Iot__StatusCodes	KEY_2;
	
	INT64				WEIGHING_1_Report;	
	INT64				WEIGHING_2_Report;
	
	INT64				WEIGHING_1_Setting;	
	INT64				WEIGHING_2_Setting;
	
	INT64 				TRASH_CAPACITY_1_Setting;
	INT64 				TRASH_CAPACITY_2_Setting;
	
	INT64 				TRASH_CAPACITY_1_Report;
	INT64 				TRASH_CAPACITY_2_Report;	
	
	Iot__StatusCodes	LED_1_Setting;
	Iot__StatusCodes	LED_2_Setting;

	Iot__StatusCodes	LED_1_Report;
	Iot__StatusCodes	LED_2_Report;	
	
	Iot__StatusCodes 	PLATE_1_Setting;
	Iot__StatusCodes 	PLATE_2_Setting;
	
	UINT32 	PLATE_1_Setting_Time;	//==开关门时长
	UINT32 	PLATE_2_Setting_Time; //==开关门时长

	Iot__StatusCodes 	PLATE_1_Report;
	Iot__StatusCodes 	PLATE_2_Report;
	

	
	Iot__StatusCodes	LIGHT_INSIDE_1_Setting;
	Iot__StatusCodes	LIGHT_INSIDE_1_Report;
	Iot__StatusCodes	LIGHT_INSIDE_2_Setting;
	Iot__StatusCodes	LIGHT_INSIDE_2_Report;
	
	Iot__StatusCodes	LIGHT_OUTSIDE_1_Setting;
	Iot__StatusCodes	LIGHT_OUTSIDE_1_Report;
	Iot__StatusCodes	LIGHT_OUTSIDE_2_Setting;
	Iot__StatusCodes	LIGHT_OUTSIDE_2_Report;


	Iot__StatusCodes 	LOCK_1_Setting;
	Iot__StatusCodes 	LOCK_2_Setting;	
	
	Iot__StatusCodes 	LOCK_1_Report;
	Iot__StatusCodes 	LOCK_2_Report;	
	
	Iot__StatusCodes 	SENSOR_1_Setting;
	Iot__StatusCodes 	SENSOR_2_Setting;	
	
	Iot__StatusCodes 	SENSOR_1_Report;
	Iot__StatusCodes 	SENSOR_2_Report;	
	
	Iot__StatusCodes	FAN_Setting;
	INT64				TEMPERATURE_Report;
	
		
	UINT32				IC_CARD;
	UINT8				IC_BLOCK_2[16];
}st_Device_Value;

extern st_Device_Value 	Device_Value;
/********************需修改部分********eeeeee***************************************************/


INT32 OTA_START(UINT8 *OtaMessage, Iot__ResultCodes *ResultCodes, Iot__ErrorCodes *ErrorCodes);
INT32 OTA_PACKET(UINT8 *OtaMessage, Iot__ResultCodes *ResultCodes, Iot__ErrorCodes *ErrorCodes);
INT32 OTA_CHECK_END(UINT8 *OtaMessage);
void SYSTEM_Reset_Loop(void);
#endif
